<?php 
session_start();
include "dbconnect.php";
if(!isset($_SESSION['Username']))
{
  header("location:index.php");

}
else 
{
	if($_SESSION['Type']!=1)
  header("location:unauthorized.php");
	//echo isset($_SESSION['Username']);
	
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Post</title>
<script src="media/js/jquery.js" type="text/javascript"></script>
<script src="media/js/jquery.datatables.js" type="text/javascript"></script>


 <link rel="stylesheet" href="jqwidgets/styles/jqx.base.css" type="text/css" />
    <link rel="stylesheet" href="jqwidgets/styles/jqx.summer.css" type="text/css" />
    <script type="text/javascript" src="scripts/jquery-2.0.3.min.js"></script>
    <script type="text/javascript" src="jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="jqwidgets/jqxexpander.js"></script>
    
  <script src="scripts/jquery-ui-1.10.3/jquery-ui-1.10.3/ui/jquery-ui.js" type="text/javascript" > </script>
   
   
    <script type="text/javascript">	
		 $(document).ready(function () {
            // Create jqxExpander
            $("#expanderman").jqxExpander({});
        });		
        $(document).ready(function () {
            // Create jqxExpander
            $("#expanderexc").jqxExpander({}); 
        });
		function courseSuggest()
		{
    				$.ajax({
						type: "POST",
                		url: "autoSuggest.php",
                		data: {option: 1, courseid: $("#courseid").val()},
					    success: function(courses){
									$("#courseid").autocomplete({
        								source: courses.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");
							   }
					});
			
		}
		function instructorSuggest()
		{
    				$.ajax({
						type: "POST",
                		url: "autoSuggest.php",
                		data: {option: 2, instructor: $("#instructor").val()},
					    success: function(instructor){
									$("#instructor").autocomplete({
        								source: instructor.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");
							   }
					});
		}
			function populateYear()
		{
			$.ajax({
						type: "POST",
                		url: "PopulateYear.php",
                		data: {term :document.getElementById("sem").value,year: document.getElementById("ayear").value},
					   success: function(courses){
									$("#ayear").autocomplete({
        								source: courses.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");
							   }
					});
		}
		function PushSingleData()
		{
			if($("#ayear").val()=="")
			{
				document.getElementById("lyear").innerHTML="Year is required";
				return;	
			}
			else
			{
				if(!$("#ayear").val().trim().match(/\b[0-9][0-9][0-9][0-9]\b/))
				{
					document.getElementById("lyear").innerHTML="Invalid year format";
					return;
					
				}
				else
				{
					document.getElementById("lyear").innerHTML="";
				}
			}
			if($("#courseid").val().trim()=='' || $("#timings").val().trim()=="" || $("#instructor").val().trim()=="" || $("#notes").val().trim()=="" || $("#totalslots").val().trim()=="" || $("#filledslots").val().trim()=="" || $("#waitingslots").val().trim()=="" || $("#section").val().trim()=="" || $("#location").val().trim()=="" || $("#day").val().trim()=="")
			{
				alert("Fill all the fields.");
				return;
			}
			if(!$("#courseid").val().trim().match(/\b[A-Z]\d\d\d\b/))
			{
				alert("Incorrect Course ID");
				return;
			}
			$("#pushdata").val(1);		
			$("#post").submit();
		}
		function PushData()
		{
			if($("#ayear").val()=="")
			{
				document.getElementById("lyear").innerHTML="Year is required";
				return;	
			}
			else
			{
				if(!$("#ayear").val().trim().match(/\b[0-9][0-9][0-9][0-9]\b/))
				{
					document.getElementById("lyear").innerHTML="Invalid year format";
					return;
					
				}
				else
				{
					document.getElementById("lyear").innerHTML="";
				}
			}
			$("#pushdata").val(0);
			var filename=document.getElementById("postcsv").value;
			if (filename.length==0)
		    { 
				alert("Select a csv file to upload.");
				return;
  			}
			if(filename.substring(filename.lastIndexOf("."))!=".csv")
			{
				alert("Select a csv file only.");
				return;
			}
			//alert(document.getElementById("ayear").value);
			$.ajax({
						type: "POST",
                		url: "CheckDataExist.php",
                		data: {term: document.getElementById("sem").value, year: document.getElementById("ayear").value},
						async:true,
					    success: function(result){
									if(result==1)
									{
										var strconfirm = confirm("Data exist for the selected term and year. Are you sure you want to override the data completely?");
										if(strconfirm==false)
										{
											return;
										}
									}
									$("#post").submit();
								 },
						error: function(){
							   	alert("Error");
							   }
					});
		}
	</script>
<style type="text/css">
@import "media/css/demo_table_jui.css";
@import "media/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>
<link href="CSS/Global.css" type="text/css" rel="stylesheet" />
<link href="CSS/Base.css" type="text/css" rel="stylesheet" />
<link href="CSS/Fixed.css" type="text/css" rel="stylesheet" />
<style>

</style>
</head>

<body style="min-width:1280px" onload="populateYear()">

<div id="branding-bar" class="gateway">
<div class="bar">
<div class="wrapper">
<h1 class="campus"><a><img  height="73" src="Resources/img/trident-tab.gif.png" width="64"/>AIMS</a></h1>
</div>
</div>
</div>
<div class="grey">
</div>
<center>
<div id="hnav" style="background:none"><!--system-region name="GLOBAL NAVIGATION"/-->

              <ul>
                <li ><a href="hireView.php">Hire</a></li>
                <li ><a href="post.php">Openings</a></li>
                <li ><a href="budget.php">Budget</a></li>
                <li ><a href="semester.php">Semester</a></li>
                <li ><a href="manage.php">Manage</a></li>
              </ul>          

</div>
</center>
<span style="width:100px;margin-left:300px;">
                <button class="hyperbutton" onclick="location.href = 'logout.php'" >logout</button>
         </span>
<div class="footbody">

<form id="post" action="PushOpeningsData.php" method="post" autocomplete="off" enctype="multipart/form-data"> 
<table cellspacing="5" cellpadding="5">
    <thead>
    <tr>
        <td>
        <label class="Header1">Post Openings</label>
        </td>
    </tr>
    </thead>
    <tbody>
    <tr/>
    <tr/>
    <tr/>
   		 		<tr>
                        <td>
                    	<label class="crimson-text">Select a semester </label>
                      </td>
                  <td>
                      <select id="sem" name="sem"  class="crimson-text" onchange="populateYear()">
                        <option selected value="fall">Fall</option>
                        <option value="spring">Spring</option>
                      </select>
                  </td> 
   			 </tr>
                  <tr>
                      <td>
                        <label class="crimson-text">Year </label>
                      </td>
                  <td>
                  		<input type="text" id="ayear" name="ayear" class="crimson-border" onkeyup="populateYear()"/>
                        	<label id="lyear" name="lyear" for="ayear"></label>
                  </td>
			</tr>
</tbody>
</table>

        <div id='expanderman'  style="margin-top:20px">
        <div>
        Post an Opening - Manually
        </div>
        <div>
        <table cellpadding="2" cellspacing="3">
        <tbody>
        <tr>
       					 <td>
                            <label style="color:#990000">Course </label>
                          </td>
                          <td>
                              <input type="text" id="courseid" name="courseid" class="crimson-border" onkeyup="courseSuggest()"/>
                          </td>
        				  <td>
                            <label style="color:#990000">Timings </label>
                          </td>
                          <td>
                              <input type="text"  id="timings" name="timings" class="crimson-border"/>
                          </td>	
        </tr>
        <tr/><tr/><tr/><tr/><tr/><tr/><tr/>
        <tr>
        <td>
                            <label style="color:#990000">Instructor </label>
                          </td>
                          <td>
                              <input type="text" id="instructor" name="instructor" class="crimson-border" onkeypress="instructorSuggest()"/>
                          </td>	
        <td><label style="color:#990000">Day </label></td>
                          <td><input type="text" id="day" name="day" class="crimson-border"/></td>	
        </tr>
        <tr/><tr/><tr/><tr/><tr/><tr/><tr/>
        <tr>
               			 <td>
                            <label style="color:#990000">Total Slots </label>
                          </td>
                          <td>
                              <input type="text" id="totalslots" name="totalslots" class="crimson-border"/>
                          </td>	
        <td><span style="color:#990000">Notes</span></td>
                          <td><input type="text" id="notes" name="notes" class="crimson-border"/></td>	
        </tr>
        <tr/><tr/><tr/><tr/><tr/><tr/><tr/>
        <tr>
          <td><label style="color:#990000">Waiting Slots </label></td>
          <td><input type="text" id="waitingslots" name="waitingslots"  class="crimson-border"/></td>
          <td><label style="color:#990000">Filled Slots</label></td>
          <td><input type="text"  id="filledslots" name="filledslots" class="crimson-border"/></td>
          </tr>
        <tr/><tr/><tr/><tr/><tr/><tr/><tr/>
        <tr>
        <td>
                            <label style="color:#990000">Section </label>	
                          </td>
                          <td>
                              <input type="text" id="section" name="section" class="crimson-border" />
                          </td>
                          <td><label style="color:#990000">Location </label></td>	
       					  <td><input type="text" id="location" name="location"  class="crimson-border"/></td>	
        </tr>
        <tr/><tr/><tr/><tr/><tr/><tr/><tr/>
        <tr>
                            <td colspan="2">
                              <input id="create" name="create" type="button" onclick="PushSingleData()" value="Create" class="crimson-text" />
                          </td>
                          
        </tr>
        </tbody>
        </table>
        </div>
        </div>
        <div id='expanderexc' style="margin-top:20px">
        <div>
        Post an Opening - File Upload
        </div>
        <div>
        <table>
        <tbody>
        <tr>
       					 <td>
                            <label style="color:#990000">Select a file </label>
                          </td>
                          <td>
                          
                          <input type="hidden" id="pushdata" name="pushdata" />
                              <input type="file" name="postcsv" id="postcsv" accept=".csv"  />
                          </td>
        </tr>
        <tr/><tr/><tr/><tr/><tr/><tr/><tr/>
        <tr>
                            <td>
                              <input id="push" name="push" type="button" onclick="PushData()" value="Create" class="crimson-text"/>
                              </form>
                          </td>
                          
        </tr>
        
        </tbody>
        </table>
        </div>
        </div>
	</div>
</body>
</html>